﻿
using System;
using System.Threading;
using System.Threading.Tasks;

namespace WSU.ChekaParallel.TestTask
{
    /// <summary>
    /// test the "run synchronously" feature of Task
    /// which can be very helpful when testing and debugging task
    /// </summary>
    static class TestSyncRun
    {
        private static void TestRunSynchronously()
        {
            Console.WriteLine("### caller thread is: {0}", Thread.CurrentThread.ManagedThreadId);
            Action<object> execBody = name =>
                                 {
                                     Thread.Sleep(1000);
                                     Console.WriteLine("task[{0}] executed on thread<{1}>",
                                         name, Thread.CurrentThread.ManagedThreadId);
                                 };

            // --------------- run the task again asynchronously
            Task asyncTask = new Task(execBody, "Async");
            asyncTask.Start();// run in the thread pool

            asyncTask.Wait();
            Console.WriteLine("--- Async run finished.\n");

            // --------------- run the task again synchronously
            Task syncTask = new Task(execBody,"Sync");
            syncTask.RunSynchronously();
            Console.WriteLine("--- Sync run finished.");
        }

        public static void TestMain()
        {
            TestRunSynchronously();
        }
    }
}